﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;

namespace Zzj.Utils
{
    /// <summary>
    /// 神思读卡器
    /// </summary>
    public class IDCardReader
    {
        /// <summary>
        /// 基本函数
        /// </summary>
        /// <param name="pCmd"></param>
        /// <param name="parg0"></param>
        /// <param name="parg1"></param>
        /// <param name="parg2"></param>
        /// <returns></returnsOpenDevice
        [DllImport("RdCard.dll")]
        private static extern int UCommand1(ref byte pCmd, ref  int parg0, ref int parg1, byte[] parg2);


        /// <summary>
        /// 初始化设备(串口:1-16,USB:1001-1016),为零自动找接口
        /// </summary>
        /// <param name="nPort">(串口:1-16,USB:1001-1016)</param>
        /// <returns></returns>
        public static bool InitCom(int nPort, out string sMsg)
        {
            sMsg = string.Empty;
            try
            {
                Byte cmd = 0x41;
                int para0 = nPort;
                int para1 = 8811;
                byte[] para2 = { 0x02, 0x27, 0x00, 0x00 };    //9986   
                int nRet = UCommand1(ref cmd, ref para0, ref para1, para2);

                if (nRet == 62171)
                {
                    sMsg = "身份证读卡器连接成功";
                    return true;
                }
                else
                    sMsg = "身份证读卡器连接失败!";
            }
            catch (Exception ex)
            {
                sMsg = "身份证读卡器连接失败,原因是:" + ex.Message;
            }
            return false;

        }

        /// <summary>
        /// 关闭设备
        /// </summary>
        /// <returns></returns>
        public static bool CloseCom(int nPort, out string sMsg)
        {
            sMsg = string.Empty;
            try
            {
                Byte cmd = 0x42;
                int para0 = nPort;
                int para1 = 8811;
                byte[] para2 = { 0x02, 0x27, 0x00, 0x00 };    //9986   
                int nRet = UCommand1(ref cmd, ref para0, ref para1, para2);

                if (nRet == 62171)
                {
                    sMsg = "端口关闭成功";
                    return true;
                }
                else
                    sMsg = "端口关闭失败!";
            }
            catch (Exception ex)
            {
                sMsg = "端口关闭失败,原因是:" + ex.Message;
            }

            return false;
        }

        /// <summary>
        /// 获取身份证信息
        /// </summary>
        /// <param name="objCardInfo"></param>
        /// <param name="nInfoType">nInfoType=1，读基本信息；nInfoType=3，读追加地址</param>
        /// <returns></returns>
        public static CardInfo ReadCardInfo(int nPort, out string sMsg, string sSavePath)
        {
            CardInfo objCardInfo = null;
            try
            {

                byte bCmd = 0x43;//验证信息
                int nPara0 = nPort;
                int nPara1 = 8811;
                byte[] nPara2 = { 0x02, 0x27, 0x00, 0x00 };    //9986   
                int nRet = UCommand1(ref bCmd, ref nPara0, ref nPara1, nPara2);// 验证卡

                if (nRet == 62171)//身份证验证成功
                {
                    bCmd = 68;// '0x44 读卡内信息
                    nPara0 = nPort;
                    nPara1 = 8811;
                    //nPara2 = { 0x02, 0x27, 0x00, 0x00 };
                    //nPara2 = 9986;
                    //char strPath = "C:\\";
                    //如果要用sSavePath，请将sSavePath转换为ANSI编码格式。
                    //因为.net中的字符串是unicode格式，所以这里使用byte类型
                    //内部逐字节进行赋值，最后一个是0
                    //
                    //如，C:\a     调用后会生成a.txt a1.txt,a.bmp a1.wlt
                    byte[] ctr ={   (byte)'C',          
                                        (byte)':',          
                                        (byte)'\\',
                                        (byte)'a',
                                        (byte)'\0'
                                    };

                    nRet = UCommand1(ref bCmd, ref nPara0, ref nPara1, ctr);// '读卡内信息


                    if (nRet == 62171 || nRet == 62172)
                    {
                        sMsg = string.Empty;
                        objCardInfo = new CardInfo();

                        //System.IO.StreamReader objStreamReader = new System.IO.StreamReader(sSavePath + @"/wx.txt", System.Text.Encoding.Default);
                        System.IO.StreamReader objStreamReader = new System.IO.StreamReader("C:\\awx.txt", System.Text.Encoding.Default);

                        objCardInfo.Name = objStreamReader.ReadLine();
                        objCardInfo.Sex = objStreamReader.ReadLine();
                        objCardInfo.Nation = objStreamReader.ReadLine();
                        objCardInfo.Birthday = objStreamReader.ReadLine();
                        objCardInfo.Address = objStreamReader.ReadLine();
                        objCardInfo.CardNo = objStreamReader.ReadLine();
                        objCardInfo.Department = objStreamReader.ReadLine();
                        objCardInfo.StartDate = objStreamReader.ReadLine();
                        objCardInfo.EndDate = objStreamReader.ReadLine();
                        objCardInfo.AddressEx = objStreamReader.ReadLine();
                        //objCardInfo.PhotoPath = sSavePath + @"/zp.bmp";
                        objCardInfo.PhotoPath = "c:\\azp.bmp";

                        string sPhotoPath = objCardInfo.PhotoPath;
                        objCardInfo.ArrPhotoByte = ImageToByteArray(sPhotoPath);//相片的字节信息 将照片转为字符穿

                        objStreamReader.Close();
                        objStreamReader.Dispose();

                        return objCardInfo;
                    }
                    else if (nRet == -5)
                        sMsg = "返回值：" + nRet + "软件未授权!";
                    else
                    {
                        sMsg = "返回值：" + nRet + "读身份证不成功";
                        //MessageBox.Show(sMsg);
                    }
                }
                else
                    sMsg = "请将身份证放置感应区域";

                return objCardInfo;
            }
            catch (Exception ex)
            {
                sMsg = "读身份证失败,原因是:" + ex.Message;
                return null;
            }

        }

        /// <summary>
        /// 将图片转换成字节
        /// </summary>
        /// <param name="selectPictureFile"></param>
        /// <returns></returns>
        
        private static Byte[] ImageToByteArray(string selectPictureFile)
        {
            Image photo = new Bitmap(selectPictureFile);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            photo.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] imagedata = ms.GetBuffer();

            ms.Close();
            ms.Dispose();
            photo.Dispose();
            return imagedata;
        }
        
    }

    /// <summary>
    /// 身份证的信息
    /// </summary>
    public class CardInfo
    {
        /// <summary>
        /// 订单编号
        /// </summary>
        public string OrderNo;
        public int Id;
        /// <summary>
        /// 身份证号码
        /// </summary>
        public string CardNo;
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name;
        /// <summary>
        /// 性别
        /// </summary>
        public string Sex;
        /// <summary>
        /// 出生日期
        /// </summary>
        public string Birthday;
        /// <summary>
        /// 地址
        /// </summary>
        public string Address;
        /// <summary>
        /// 追加地址
        /// </summary>
        public string AddressEx;
        /// <summary>
        /// 发卡机关
        /// </summary>
        public string Department;
        /// <summary>
        /// 证件开始日期
        /// </summary>
        public string StartDate;
        /// <summary>
        /// 证件结束日期
        /// </summary>
        public string EndDate;
        /// <summary>
        /// 民族
        /// </summary>
        public string Nation;
        /// <summary>
        /// 分店编号
        /// </summary>
        public int ChainID;
        /// <summary>
        /// 相片路径
        /// </summary>
        public string PhotoPath;
        /// <summary>
        /// 相片的字节信息
        /// </summary>
        public byte[] ArrPhotoByte;
        /// <summary>
        /// 时时图片字节信息
        /// </summary>
        public byte[] PhoTimeByte;
        /// <summary>
        /// 操作时间
        /// </summary>
        public DateTime OpeTime;
        /// <summary>
        /// 操作人
        /// </summary>
        public int UserID;
        /// <summary>
        /// 操作人
        /// </summary>
        public string UserName;
        /// <summary>
        /// 入住人数
        /// </summary>
        public int nMebCount;
        /// <summary>
        /// 提交状态,1:插入成功；0：插入失败
        /// </summary>
        public string nState;
        /// <summary>
        /// 备注信息
        /// </summary>
        public string sRemark;
        /// <summary>
        /// 客户端名称
        /// </summary>
        public string ClientName;
    }
}
